package com.newer.weatherforecast.util;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.newer.weatherforecast.entity.CurrentWeather;
import com.newer.weatherforecast.entity.ForecastWeather;
import com.newer.weatherforecast.ui.R;
import com.newer.weatherforecast.xml.CnXmlHandler;
import com.newer.weatherforecast.xml.EnXmlHandler;

public class WeatherUtil {
	private CurrentWeather currentWeather = null;
	private List<ForecastWeather> forecastWeather = null;
	public void getCityWeather(URL url){
    	try {
    		HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    		httpURLConnection.setDoInput(true);
    		httpURLConnection.connect();
    		InputStream is = httpURLConnection.getInputStream();
    		BufferedInputStream bis = new BufferedInputStream(is);
			SAXParserFactory saxFactory = SAXParserFactory.newInstance();
			XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
			String path = url.toString();
    		if(path.substring(path.indexOf("=")+1, path.indexOf("&")).equals("en")){
    			System.out.println("进入英文解析");
    			EnXmlHandler myHandler = new EnXmlHandler();
    			xmlReader.setContentHandler(myHandler);
    			xmlReader.parse(new InputSource(new InputStreamReader(bis,"GBK")));
    			currentWeather = myHandler.getCurrentWeather();
    			forecastWeather = new ArrayList<ForecastWeather>();
    			forecastWeather = myHandler.getForecastWeather();
    		}else if(path.substring(path.indexOf("=")+1, path.indexOf("&")).equals("zh_cn")){
    			System.out.println("进入中文解析");
    			CnXmlHandler myHandler = new CnXmlHandler();
    			xmlReader.setContentHandler(myHandler);
    			xmlReader.parse(new InputSource(new InputStreamReader(bis,"GBK")));
    			currentWeather = myHandler.getCurrentWeather();
    			forecastWeather = myHandler.getForecastWeather();
    		}
			bis.close();
			is.close();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	public CurrentWeather getCurrentWeather(){
		return currentWeather;
	}
	public List<ForecastWeather> getForecastWeathers(){
		return forecastWeather;
	}
	public int getWeatherImage(String path){
		if (path.equals("sunny")) {
			return R.drawable.sunny;
		} else if(path.equals("chance_of_rain") || 
				path.equals("chance_of_storm")){
			return R.drawable.chance_of_rain;
			
		}else if(path.equals("chance_of_snow")){
			return R.drawable.chance_of_snow;
		}else if(path.equals("cloudy")||path.equals("cn_cloudy")){
			return R.drawable.cloudy;
		}else if (path.equals("dust")) {
			return R.drawable.dust;
		}else if (path.equals("fog") || path.equals("haze")) {
			return R.drawable.fog;
		}else if (path.equals("mist") || path.equals("rain")||path.equals("cn_heavyrain")) {
			return R.drawable.rain;
		}else if (path.equals("mostly_cloudy") || 
				path.equals("mostly_sunny") || path.equals("partly_cloudy")) {
			return R.drawable.mostly_cloudy;
		}else if (path.equals("sleet") || path.equals("snow")
				|| path.equals("icy")) {
			return R.drawable.snow;
		}else if (path.equals("storm")) {
			return R.drawable.storm;
		}else if (path.equals("thunderstorm")) {
			return R.drawable.thunderstorm;
		}
		return 0 ;
	}
}
